<?php

class MySqlHelper
{
    private $con;
    
    private $curDb;
    
    public $host;
    public $port;
    public $u;
    public $p;
    
    
    
    public function MySqlHelper($host,$port,$u,$p)
    {
        $this->host=$host;
        $this->port=$port;
        $this->u=$u;
        $this->p=$p;
    }
    
    public function connect()
    {
        $this->con  = mysql_connect($this->host.":".$this->port,$this->u,$this->p);
        if(!$this->con)
        {
            return false;
        }
        return true;
    }
    
    public function getSchemas()
    {
        $qry = "SHOW SCHEMAS";
        $result = mysql_query($qry,$this->con);
        $schemas=array();
        while($row = mysql_fetch_row($result))
        {
            $schemas[]=$row[0];
        }
        return $schemas;
    }
    
    public function getTables($dbName)
    {
        $qry = "USE `$dbName`";
        if(mysql_query($qry,$this->con))
        {
            $this->curDb = $dbName;
            $qry = "SHOW TABLES";
            $result = mysql_query($qry,$this->con);
            $tables = array();
            while($row = mysql_fetch_array($result))
            {
                $tables[] = $row[0];
            }
            return $tables;
        }
    }
    
    public function descTable($dbName,$tableName)
    {
        if($this->curDb != $dbName)
        {
            $qry = "USE `$dbName`";
            if(mysql_query($qry))
            {
                $this->curDb = $dbName;
            }
        }
        
        $qry = "DESC `$tableName`";
        $result = mysql_query($qry,$this->con);
        $fields = array();
        while($row = mysql_fetch_row($result))
        {
            $fields[] = $row;
        }
        return $fields;
    }
}
?>
